package com.adam.example.aggregation.metric;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder;

import java.net.UnknownHostException;

public class CardinalityAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders
                .cardinality("agg").field("language");
        SearchResponse searchResponse = transportClient.prepareSearch("test_books")
                .setSize(0).addAggregation(cardinalityAggregationBuilder).get();
        Cardinality cardinality = searchResponse.getAggregations().get("agg");
        System.out.println(cardinality.getValue());

        transportClient.close();
    }

}
